<?php
namespace app\dsadmin\controller;
use app\dsadmin\model\TousuModel;
use think\Db;

class Tousu extends Base
{
    public function index()
    {
        $key = input('key');
        $map = [];

        if ($key && $key !== "")
        {
            $map['title'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 10; // 获取总条数
        $count = Db('ts')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $Tousu = new TousuModel();
        $lists = $Tousu->getTousuAll($map, $Nowpage, $limits);
        
       
         
         foreach ($lists as &$v){
             
            //   $order = Db::name('ip')->where(['uopenid' => $v['uopenid']])->find();
             
              $order = Db::name('ip')->where(['uopenid' => $v['uopenid'],'ip' => $v['ip']])->order('id desc')->find();
              
              $name = Db::name('gongyou')->where(['id' => $order['zyid']])->value('name');
             
             if(empty($name)){
                   $name = Db::name('siyou')->where(['id' => $order['zyid']])->value('name');
             }
             if($order){
                 $v['zf']      = '已付款';
                 $v['time']    = date("Y-m-d H:i:s",$order['shijian']);
                 $v['order']   = $order['ddh2'];
             }else{
                 
                 $v['zf']    = '未付款';
                 $v['order'] = '';
                 $v['time']    = '';
                 
             }
            
             $ip = $v['ip'];
            //  $url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='".$ip."'&co=&resource_id=6006&t=1597758495883&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110204112591273233335_1597757755399&_=1597757755440";
            
            //  $arr = file_get_contents("compress.zlib://".$url);
            //  $content = iconv("GBK", "UTF-8", $arr);
            //  $result1 = strstr($content,"titlecont",1);
            //  $result2 = strstr($result1 ,"location");
            //  $result3 = substr($result2 , 11);
            //  $result3 = str_replace(",","",$result3);
             $v['ip_name'] = "中国";
             $v['name'] = $name;
            
             
         }
         
     
        
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        if (input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }
    
    

 

    public function add_Tousu()
    {
        if (request()->isAjax())
        {
            $param = input('post.');
            $param['closed'] = 0;
            $Tousu = new TousuModel();
            $flag = $Tousu->insertTousu($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }

    public function del()
    {
        $id = input('param.id');
        $Tousu = new TousuModel();
        $flag = $Tousu->delTousu($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    public function jz_tousu()
    {
        $id = input('param.id');
        $Tousu = new TousuModel();
        $flag = $Tousu->jzTousu($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    public function yx_tousu()
    {
        $id = input('param.id');
        $Tousu = new TousuModel();
        $flag = $Tousu->yxTousu($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
}
